package com.gxt.controller;

import java.util.UUID;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import com.gxt.pojo.Users;
import com.gxt.pojo.VO.UsersVO;
import com.gxt.service.UserService;
import com.gxt.utils.IMoocJSONResult;
import com.gxt.utils.MD5Utils;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;

@RestController
@Api(value = "用户注册登录的接口", tags = { "注册和登录的controller" })
public class RegistLoginController extends BasicController {
	@Autowired
	private UserService userservice;
	
	public UsersVO setUserRedisSessionToken(Users userModel) {
		String uniqueToken = UUID.randomUUID().toString();
		redis.set(USER_REDIS_SESSION + ":" + userModel.getId(), uniqueToken, 1000 * 60 * 30);
		UsersVO userVO = new UsersVO();
//		BeanUtil进行拷贝
		BeanUtils.copyProperties(userModel, userVO);
		userVO.setUserToken(uniqueToken);

		return userVO;
	}
	
	@ApiOperation(value = "用户注册", notes = "用户注册的接口")
	@PostMapping("/regist")
	public IMoocJSONResult Hello(@RequestBody Users user) throws Exception {
//		1.判断用户名和密码必须不为空
		if (StringUtils.isBlank(user.getUsername()) || StringUtils.isBlank(user.getPassword())) {
			return IMoocJSONResult.errorMsg("用户名和密码不能为空");
		}
//		2.判断用户名是否存在
		boolean userNameIsExist = userservice.queryUsernameIsExist(user.getUsername());
//		3.保存用户注册信息
		if (!userNameIsExist) {
			user.setNickname(user.getNickname());
			user.setPassword(MD5Utils.getMD5Str(user.getPassword()));
			user.setFansCounts(0);
			user.setFollowCounts(0);
			user.setReceiveLikeCounts(0);
			userservice.saveUser(user);
		} else {
			return IMoocJSONResult.errorMsg("用户名已经存在，请换一个再试");
		}
//		注册成功
		user.setPassword(null);
//		String uniqueToken = UUID.randomUUID().toString();
//		redis.set(USER_REDIS_SESSION + ":" + user.getId(), uniqueToken, 1000 * 60 * 30);
//		UsersVO userVO = new UsersVO();
////		BeanUtil进行拷贝
//		BeanUtils.copyProperties(user, userVO);
//		userVO.setUserToken(uniqueToken);

		return IMoocJSONResult.ok(setUserRedisSessionToken(user));
	}

	@ApiOperation(value = "用户登录", notes = "用户登录的接口")
	@PostMapping("/login")
	public IMoocJSONResult login(@RequestBody Users user) throws Exception {
		String username = user.getUsername();
		String password = user.getPassword();
//		1.判断用户名和密码必须不为空
		if (StringUtils.isBlank(user.getUsername()) || StringUtils.isBlank(user.getPassword())) {
			return IMoocJSONResult.errorMsg("用户名和密码不能为空");
		}

//		2.判断用户名是否存在
		Users userResult = userservice.queryUserForLogin(username, MD5Utils.getMD5Str(password));

//		3.返回
		if (userResult != null) {
			userResult.setPassword(null);
			return IMoocJSONResult.ok(setUserRedisSessionToken(userResult));
		} else {
			return IMoocJSONResult.errorMsg("用户名或密码不正确，请重试！");
		}
	}

	@ApiOperation(value = "用户注销", notes = "用户注销的接口")
	@ApiImplicitParam(name = "userId",value = "用户Id",required = true,
						dataType = "String",paramType = "query")
	@PostMapping("/logout")
	public IMoocJSONResult logout(String userId) throws Exception {
		redis.del(USER_REDIS_SESSION + ":" + userId);
		
		return IMoocJSONResult.ok("注销成功！");
	}

	
}
